package com.ruoyi.web.controller.interview;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.interview.domain.InterviewDimension;
import com.ruoyi.interview.domain.InterviewPosition;
import com.ruoyi.interview.service.*;
import com.ruoyi.web.util.ChatUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interview.domain.InterviewQuestion;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 面试问题Controller
 * 
 * @author z5
 * @date 2025-06-16
 */
@RestController
@RequestMapping("/interview/question")
public class InterviewQuestionController extends BaseController
{
    @Autowired
    private IInterviewQuestionService interviewQuestionService;
    @Autowired
    private IInterviewDimensionService interviewDimensionService;
    @Autowired
    private IInterviewPositionService interviewPositionService;
    @Autowired
    private IInterviewRecordService interviewRecordService;

    private static final int QUESTION_NUMBER = 5;
    /**
     * 查询面试问题列表
     */
    @PreAuthorize("@ss.hasPermi('interview:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(InterviewQuestion interviewQuestion)
    {
        startPage();
        List<InterviewQuestion> list = interviewQuestionService.selectInterviewQuestionList(interviewQuestion);
        return getDataTable(list);
    }

    /**
     * 导出面试问题列表
     */
    @PreAuthorize("@ss.hasPermi('interview:question:export')")
    @Log(title = "面试问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InterviewQuestion interviewQuestion)
    {
        List<InterviewQuestion> list = interviewQuestionService.selectInterviewQuestionList(interviewQuestion);
        ExcelUtil<InterviewQuestion> util = new ExcelUtil<InterviewQuestion>(InterviewQuestion.class);
        util.exportExcel(response, list, "面试问题数据");
    }

    /**
     * 获取面试问题详细信息
     */
    @PreAuthorize("@ss.hasPermi('interview:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(interviewQuestionService.selectInterviewQuestionById(id));
    }

    /**
     * 新增面试问题
     */
    @PreAuthorize("@ss.hasPermi('interview:question:add')")
    @Log(title = "面试问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InterviewQuestion interviewQuestion)
    {
        return toAjax(interviewQuestionService.insertInterviewQuestion(interviewQuestion));
    }

    /**
     * 修改面试问题
     */
    @PreAuthorize("@ss.hasPermi('interview:question:edit')")
    @Log(title = "面试问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InterviewQuestion interviewQuestion)
    {
        return toAjax(interviewQuestionService.updateInterviewQuestion(interviewQuestion));
    }

    /**
     * 删除面试问题
     */
    @PreAuthorize("@ss.hasPermi('interview:question:remove')")
    @Log(title = "面试问题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(interviewQuestionService.deleteInterviewQuestionByIds(ids));
    }

    /**
     * 生成面试问题
     * @param positionId
     * @return
     */
    @GetMapping("/genQuestion")
    public AjaxResult genQuestionByPosition(Long positionId) {
        // 记录方法调用日志
        System.out.println("genQuestionByPosition 方法被调用，岗位 ID: " + positionId);
        //1、根据前端传过来的岗位id查询岗位信息
        InterviewPosition interviewPosition = interviewPositionService.selectInterviewPositionById(positionId);
        //2、查询岗位对应的维度信息
        List<InterviewDimension> dimensionList = interviewDimensionService.selectInterviewDimensionByPositionId(positionId);
        String dimensions = genDimensionPrompt(dimensionList);
        //3、根据岗位的名称生成模拟面试的问题
        IQuestionGenerateService service = ChatUtil.getService(IQuestionGenerateService.class);
        String message = service.generateQuestionByPosition(interviewPosition.getName(), QUESTION_NUMBER,dimensions);
        // 4、新增一条面试记录，解析记录中的问题，保存到数据库中
        Long recordId = interviewRecordService.generateRecord(positionId,message);
        return AjaxResult.success(recordId);
    }

    /**
     * 根据维度列表，生成维度的提示词  [维度名称1(权重)]
     * @param dimensionList
     * @return
     */
    private String genDimensionPrompt(List<InterviewDimension> dimensionList) {
        return dimensionList.stream().map(dimension -> {
            return "[" + dimension.getName() + "(" + dimension.getWeight() + ") ]";
        }).collect(Collectors.joining("、"));
    }

    /**
     * 根据面试记录查询问题列表
     * @param recordId
     * @return
     */
    @GetMapping("/getQuestions")
    public AjaxResult getQuestions(Long recordId) {
        InterviewQuestion queryVo = new InterviewQuestion();
        queryVo.setRecordId(recordId);
        List<InterviewQuestion> interviewQuestions = interviewQuestionService.selectInterviewQuestionList(queryVo);
        return AjaxResult.success(interviewQuestions);
    }
}
